Using pixel pattern resources, you can create multicolored patterns for the pen pattern, for the background pattern, and for fill patterns.
To set the pixel pattern to be used by the graphics pen in the current color graphics port, you use the PenPixPat procedure. To assign a pixel pattern as the background pattern, you use the BackPixPat procedure; this causes the ScrollRect procedure and the shape-erasing procedures (for example, EraseRect ) to fill the background with your pixel pattern. To fill shapes with a pixel pattern, you use the FillCRect , FillCRoundRect , FillCOval , FillCArc , FillCPoly , and FillCRgn procedures.
Because a pixel pattern already contains color, Color QuickDraw ignores the foreground and background colors when your application uses these routines to draw with a pixel pattern. Color QuickDraw also ignores the pen mode by drawing the pixel pattern directly onto the pixel image.
When you use the PenPat or BackPat procedure in a color graphics port, Color QuickDraw constructs a pixel pattern equivalent to the bit pattern you specify to PenPat or BackPat . The pen pattern or background pattern you thereby specify always uses the graphics port's current foreground and background colors. The PenPat and BackPat procedures are described in the chapter "QuickDraw Drawing."
A pixel pattern resource is a resource of type 'ppat' . You typically use a high-level tool such as the ResEdit application, available through APDA, to create 'ppat' resources. Figure 4-10 illustrates a ResEdit window displaying an application's 'ppat' resource with resource ID 128.
Figure 10 Using ResEdit to create a pixel pattern resource
As shown in this figure, you should also define an analogous, black-and-white bit pattern (described in the chapter "QuickDraw Drawing") to be used when this pattern is drawn into a basic graphics port. This bit pattern is stored within the pixel pattern resource.
After using ResEdit to define a pixel pattern, you can then use the DeRez decompiler to convert your 'ppat' resources into Rez input when necessary. (The DeRez resource decompiler and the Rez resource compiler are part of Macintosh Programmer's Workshop [MPW], which is available through APDA.) Listing 4-3 shows the Rez input created from the 'ppat' resource created in Figure 4-10 .
Listing 3 Rez input for a pixel pattern resource
resource 'ppat' (128) {
$"0001 0000 001C 0000 004E 0000 0000 FFFF"
$"0000 0000 8292 1082 9210 8292 0000 0000"
$"8002 0000 0000 0008 0008 0000 0000 0000"
$"0000 0048 0000 0048 0000 0000 0002 0001"
$"0002 0000 0000 0000 005E 0000 0000 1212"
$"4848 1212 4848 1212 4848 1212 4848 0000"
$"0000 0000 0002 0000 AAAA AAAA AAAA 0001"
$"2222 2222 2222 0002 7777 7777 7777"
};
To retrieve the pixel pattern stored in a 'ppat' resource, you can use the GetPixPat function. Listing 4-4 uses GetPixPat to retrieve the 'ppat' resource created in Listing 4-3 . To assign this pixel pattern to the graphics pen, Listing 4-4 uses the PenPixPat procedure.
Listing 4 Using pixel patterns to paint and fill
PROCEDURE MyPaintPixelPatternRects;
VAR
firstRect, secondRect: Rect;
myPenPattern, myFillPattern: PixPatHandle;
BEGIN
myPenPattern := GetPixPat(128); {get a pixel pattern}
PenPixPat(myPenPattern); {assign the pattern to the pen}
SetRect(firstRect, 20, 20, 70, 70);
PaintRect(firstRect); {paint with the pen's pixel pattern}
DisposePixPat(myPenPattern); {dispose of the pixel pattern}
myFillPattern := GetPixPat(129); {get another pixel pattern}
SetRect(secondRect, 90, 20, 140, 70);
FillCRect(secondRect, myFillPattern); {fill with this pattern}
DisposePixPat(myFillPattern); {dispose of the pixel pattern}
END;
Listing 4-4 uses the PaintRect procedure to draw a rectangle. The rectangle on the left side of Figure 4-11 illustrates the effect of painting a rectangle with the previously defined pen pattern.
Figure 11 Painting and filling rectangles with pixel patterns
The rectangle on the right side of Figure 4-11 illustrates the effect of using the FillCRect procedure to fill a rectangle with another previously defined pen pattern. The GetPixPat function is used to retrieve the pixel pattern defined in the 'ppat' resource with resource ID 129. This pixel pattern is then specified to the FillCRect procedure.